package com.cloudlead.forwardAction.service.impl;

import com.cloudlead.commonData.service.TbBookingSpaceOrderService;
import com.cloudlead.commonData.service.TbBookingSpaceReceiptService;
import com.cloudlead.commonData.service.TbContainerInfoService;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.vo.TbBookingSpaceOrder;
import com.cloudlead.commonData.vo.TbBookingSpaceReceipt;
import com.cloudlead.commonData.vo.TbContainerInfo;
import com.cloudlead.commonData.vo.TbCustomerOrder;
import com.cloudlead.forwardAction.service.BookingSpaceReceiptEditSer;
import com.cloudlead.forwardService.vo.MoveContainerVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/9/6.
 */
@Service
public class BookingSpaceReceiptEditSerImpl implements BookingSpaceReceiptEditSer {

    @Autowired
    TbBookingSpaceReceiptService tbBookingSpaceReceiptService;
    @Autowired
    TbCustomerOrderService  tbCustomerOrderService;
    @Autowired
    TbBookingSpaceOrderService  tbBookingSpaceOrderService;
    @Autowired
    TbContainerInfoService  tbContainerInfoService;

    @Override
    @Transactional
    public String createBookingSpaceReceipt(String bookingSpaceOrderId, String trackingNo, String common) {
        /**
         * 生成订舱单回执
         */
        TbBookingSpaceReceipt  bsr = new TbBookingSpaceReceipt();
            bsr.setTrackingNo(trackingNo);//运单号
            bsr.setCommon(common);//备注
            bsr.setFlag("0");//删除标记
            bsr.setCreateTime(new DateTime());//创建时间
            TbBookingSpaceOrder bso = tbBookingSpaceOrderService.view(bookingSpaceOrderId);//查询订舱单
                if(bso==null){
                    return "订舱单ID 异常！";
                }else{
                    bsr.setTbBookingSpaceOrder(bso);
                    tbBookingSpaceReceiptService.create(bsr);//订舱回执创建
                    if(bso.getTbCustomerOrder()!=null&&bso.getTbCustomerOrder().getId()!=null){//客户订单不为空
                        TbContainerInfo ci = new TbContainerInfo();
                            TbCustomerOrder co = new TbCustomerOrder();
                                co.setId(bso.getTbCustomerOrder().getId());//添加订单ID
                            ci.setTbCustomerOrder(co);//添加订单对象用于查询
                        List<TbContainerInfo> lci= tbContainerInfoService.list(ci,null);
                        if(lci.size()>0){
                            for(Iterator<TbContainerInfo> it = lci.iterator();it.hasNext();) {//遍历
                                TbContainerInfo tci = it.next();
                                    tci = tbContainerInfoService.view(tci.getId());//得到箱信息
//                                    tci.setBookingSpaceRecei(bsr);//箱加入订舱回执
                                    tbContainerInfoService.update(tci);//保存箱信息
                            }
                        }else{
                            tbBookingSpaceReceiptService.delete(bsr.getId());//删除回执
                            return "没有对应箱信息！";
                        }
                    }else{
                        return "订舱单没有关联对应订单！";
                    }
                }
        return "success";
    }

    @Override
    @Transactional
    public String createBookingSpaceReceiptMoveContainer(MoveContainerVo vo) {
        String containerIdList = vo.getContainerIdList();//得到箱号Id
        String[] containerId;
        if(containerIdList!=null&&(!"".equals(containerIdList))) {
            containerId = containerIdList.split("&");//拆分箱号Id
            if(vo.getBookingSpaceOrderId()!=null&&(!"".equals(vo.getBookingSpaceOrderId()))){
                /**
                 * 生成订舱单回执
                 */
                TbBookingSpaceReceipt bsr = new TbBookingSpaceReceipt();
                if(vo.getToReceiptId()!=null&&(!"".equals(vo.getToReceiptId()))) {
                    bsr=tbBookingSpaceReceiptService.view(vo.getToReceiptId());//查询接收回执
                }else{//创建订舱单回执
                    bsr.setCommon(vo.getCommon());//备注
                    bsr.setTrackingNo(vo.getTrackingNo());//运单号
                    bsr.setFlag("0");//删除标记
                    bsr.setCreateTime(new DateTime());//创建时间
                    TbBookingSpaceOrder bso = new TbBookingSpaceOrder();
                    bso.setId(vo.getBookingSpaceOrderId());//添加订舱单ID
                    bsr.setTbBookingSpaceOrder(bso);//添加订舱单
                    tbBookingSpaceReceiptService.create(bsr);//保存订舱单
                }
                /**
                 * 修改对应箱的订舱回执
                 */
                for(String cId:containerId) {
                    TbContainerInfo tci = tbContainerInfoService.view(cId);//查询箱
//                        tci.setBookingSpaceRecei(bsr);//替换订舱回执
                    tbContainerInfoService.update(tci);//保存箱信息
                }
                /**
                 * 检查原回执是否删除
                 * 当该回执对应箱为0时删除
                 */
                TbContainerInfo ti = new TbContainerInfo();
                    TbBookingSpaceReceipt tb = new TbBookingSpaceReceipt();
                        tb.setId(vo.getOldReceiptId());//添加原回执ID
 //                   ti.setBookingSpaceRecei(tb);//添加原回执对象用于查询
                List<TbContainerInfo> lci =  tbContainerInfoService.list(ti,null);//查询原回执关联箱
                if(lci.size()<1){//当原回执对应箱为0 删除该回执
                    TbBookingSpaceReceipt hz = tbBookingSpaceReceiptService.view(vo.getOldReceiptId());//查询移出回执
                        hz.setFlag("1");//设1表示删除
                    tbBookingSpaceReceiptService.update(hz);
                }
            }
        }else{
            return "至少选择一条箱记录！";
        }
        return "success";
    }
}
